package util.exact;

import bean.State;
import bean.Worker;

import java.util.HashSet;
import java.util.Iterator;

public class MergeState {
    public static void main(String[] args) {

    }




    public static State mergeState(State workerState, State state) {

        /*  第 1 种情况
        先判断两个state是否相等，如果相等则判断哪个成本低，如果workerState成本低则直接返回，
        否则返回空
         */
        if ( IsSameState.isSameState(workerState,state) ) {
            if ( workerState.getCost() < state.getCost() ) {
                return workerState;
            }else {
                return null;
            }
        }else {
            /*  第 2 种情况
            目前只考虑两个状态不同，先不看包含情况
            此时只需要合并两个state
             */
            State newState = new State();
            //设置工人
            HashSet<Worker> workersNew = new HashSet<>();
            //states中的工人们
            Iterator<Worker> iteratorWorkers = state.getWorkers().iterator();
            while ( iteratorWorkers.hasNext() ) {
                workersNew.add(iteratorWorkers.next());
            }
            //workerState中的一个工人
            Iterator<Worker> iteratorWorker = workerState.getWorkers().iterator();
            while ( iteratorWorker.hasNext() ) {
                workersNew.add(iteratorWorker.next());
            }
            newState.setWorkers(workersNew);

            //设置技能
            HashSet<String> skillsNew = new HashSet<>();
            //states中的技能
            Iterator<String> iteratorSkills = state.getSkills().iterator();
            while ( iteratorSkills.hasNext() ) {
                skillsNew.add(iteratorSkills.next());
            }
            //workerState中的技能
            Iterator<String> iteratorSkill = workerState.getSkills().iterator();
            while ( iteratorSkill.hasNext() ) {
                skillsNew.add(iteratorSkill.next());
            }
            newState.setSkills(skillsNew);

            //设置成本
            double costNew = 0;
            Iterator<Worker> iteratorNewState = newState.getWorkers().iterator();
            while ( iteratorNewState.hasNext() ) {
                costNew = costNew + iteratorNewState.next().getPayoff();
            }
            newState.setCost(costNew);

            return newState;


        }
    }

}
